
from flask_sqlalchemy import SQLAlchemy
from app import db


import datetime

# import datetime
# # unique=True 唯一的
# # primary_key=True 主键
# # nullable=False 不为空

# 用户
class User(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    account = db.Column(db.String(12), nullable=False,unique=True)
    nickname = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(255), nullable=False)
    user_image = db.Column(db.String(252),nullable=True)
    create_time = db.Column(db.DateTime(), default=datetime.datetime.now())
    def __init__(self, account, nickname, password, user_image):
        self.account = account
        self.nickname = nickname
        self.password = password
        self.user_image = user_image

    def __repr__(self):
        return "<账号：{}>".format(self.account)

# 图书
class Book(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    bookID = db.Column(db.String(255), nullable=False,unique=True)
    bookName = db.Column(db.String(255), nullable=False)
    author = db.Column(db.String(255), nullable=False)
    publishing = db.Column(db.String(255), nullable=False)
    create_time = db.Column(db.DateTime(), default=datetime.datetime.now())

    def __init__(self, bookID, bookName, author, publishing):
        self.bookID = bookID
        self.bookName = bookName
        self.author = author
        self.publishing = publishing

    def getDict(self):
        return({'bookID':self.bookID,'bookName':self.bookName,'author':self.author,'publishing':self.publishing})

    def __repr__(self):
        return "<书名：{}>".format(self.bookName)


# 书架
class Bookrack(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    account = db.Column(db.String(12), nullable=False)
    bookID = db.Column(db.String(255), nullable=False)
    bookName = db.Column(db.String(255), nullable=False)
    create_time = db.Column(db.DateTime(), default=datetime.datetime.now())
    
    def __init__(self, account, bookID, bookName):
        self.account = account
        self.bookID = bookID
        self.bookName = bookName

    def __repr__(self):
        return "<书名：{0}， 图书：{1}>".format(self.bookName,self.bookID)